import { message } from 'antd'
import {
  getOrganizTree,
  addOrganizTree,
  searchOrganizTree,
  editOrganizTree,
  deleteOrganizTree,
  getFileDownloadUrl,
  getOrgCompany,
  joinCompany
} from '@/services/api';
// import { getURLparams } from '@/utils/utils'

export default {
  namespace: 'app',
  state: {
    locationPathname: '',
    urlParams: "",
    imgUrlList: "",
    singleOriginal: "",
    organizaTreeList: [],
    organizaTreelistMap: [],
    orgCompanyList: {},
    joinOrg: {},
  },

  subscriptions: {
    setupHistory({ dispatch, history }) {
      history.listen((location) => {
        dispatch({
          type: 'updateState',
          payload: {
            urlParams: location.query.id,
            locationPathname: location.pathname,
          },
        })
      })
    },
  },

  effects: {
    * getOrganizTree({ payload }, { call, put }) {//获取组织树
      const { code, msg, data } = yield call(getOrganizTree, payload);
      if (code === 1 && data) {
        // console.log(`organizaTreelist------------>`, code, data)
        let dataList = []
        const generateList = (data) => {
          for (let i = 0; i < data.length; i++) {
            const node = data[i];
            dataList.push(node);
            if (node && node.children && node.children.length !== 0) {
              generateList(node.children);
            }
          }
        };
        generateList([data])
        yield put({
          type: 'updateState',
          payload: {
            organizaTreeList: [data],
            organizaTreelistMap: dataList
          },
        });
      } else {
        message.error(msg)
      }
      // console.log(`getOrganizTree------------>`, data)
    },
    * addOrganizTree({ payload }, { call, put }) {//新增组织树
      const { resolve } = payload;
      const { code, msg, data } = yield call(addOrganizTree, payload);
      !!resolve && resolve(code);// 通过resolve返回数据，返回的数据将在Promise的then函数中获取到
      if (code === 1) {
        message.success(msg)
      } else {
        message.error(msg)
      }
    },
    * searchOrganizTree({ payload }, { call, put }) {//查询某个组织
      const { resolve } = payload;
      const { code, msg, data } = yield call(searchOrganizTree, payload);
      !!resolve && resolve({ code: code, data: data });// 通过resolve返回数据，返回的数据将在Promise的then函数中获取到 
      if (payload.orgId) {
        if (code === 1 && data) {
          yield put({
            type: 'updateState',
            payload: {
              singleOriginal: data
            },
          });
        } else {
          message.error(msg)
          // yield put({ type: 'updateState', payload: { singleOriginal: "" }, });
        }
      }
    },
    * editOrganizTree({ payload }, { call, put }) {//编辑组织树
      const { resolve } = payload;
      const { code, msg, data } = yield call(editOrganizTree, payload);
      !!resolve && resolve(code);// 通过resolve返回数据，返回的数据将在Promise的then函数中获取到
      if (code === 1) {
        message.success(msg)
      } else {
        message.error(msg)
      }
    },
    * deleteOrganizTree({ payload }, { call, put }) {//删除组织树
      const { resolve } = payload;
      const { code, msg, data } = yield call(deleteOrganizTree, payload);
      !!resolve && resolve(code);// 通过resolve返回数据，返回的数据将在Promise的then函数中获取到
      if (code === 1) {
        message.success(msg)
      } else {
        message.error(msg)
      }
    },
    * getFileDownloadUrl({ payload }, { call, put }) {//获取文件地址
      const { code, msg, data } = yield call(getFileDownloadUrl, payload);
      if (code === 1 && data) {
        if (data.length > 0) {
          yield put({
            type: 'updateState',
            payload: {
              imgUrlList: data
            },
          });
        }
      } else {
        message.error(msg)
      }
    },
    //--------------------组织内公司-----------------------
    * getOrgcompanyList({ payload }, { call, put }) {
      const { code, msg, data } = yield call(getOrgCompany, payload);
      if (code === 1) {
        yield put({
          type: 'updateState',
          payload: {
            orgCompanyList: data
          },
        });
      } else {
        message.error(msg)
      }
    },
    * joinCompany({ payload }, { call, put }) {
      const { resolve } = payload
      const { code, msg, data } = yield call(joinCompany, payload);
      !!resolve && resolve(code);// 通过resolve返回数据，返回的数据将在Promise的then函数中获取到 
      if (code === 1) {
        yield put({
          type: 'updateState',
          payload: {
            joinOrg: data
          },
        });
      } else {
        message.error(msg)
      }
    },




  },

  reducers: {
    updateState(state, { payload }) {
      return {
        ...state,
        ...payload,
      }
    },
  },
};
